from controllers.common import *


def gen_troop_control(all_data, root):
    current_data = all_data['troop_control']
    result = {}

    result['default'] = [current_data['default']]

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (epoch_parsed_success, epoch_parsed_result) = SafeToSidList(current_line['epoch_id'])
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if epoch_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析时代 id 失败：{current_line['epoch_id']}"
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': epoch_parsed_result,
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['troop_control'] = result
    return 'success', ''


def gen_event_svr_conf(all_data, root):
    current_data = all_data['event_svr_conf']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        epoch_parsed_result = current_line['epoch_id']
        sid_list_from, sid_list_to = current_line['sid_list_from'], current_line['sid_list_to']
        special.append({
            'epoch_id': epoch_parsed_result,
            'event_sid': [int(sid_list_from), int(sid_list_to)]
        })
    result['special'] = special
    root['event_svr_conf'] = result
    return 'success', ''


def gen_treasure_control(all_data, root):
    current_data = all_data['treasure_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['treasure_control'] = result
    return 'success', ''


def gen_a3_treasure_open_control(all_data, root):
    current_data = all_data['a3_treasure_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['treasure_pool_id'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['a3_treasure_open_control'] = result
    return 'success', ''


def gen_rebel_control(all_data, root):
    current_data = all_data['rebel_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result,
            'open_time': current_line['open_time']
        })
    result['special'] = special
    root['rebel_control'] = result
    return 'success', ''


def gen_search_rebel_control(all_data, root):
    current_data = all_data['search_rebel_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result,
            'open_time': current_line['open_time']
        })
    result['special'] = special
    root['search_rebel_control'] = result
    return 'success', ''


def gen_gang_of_bandits_control(all_data, root):
    current_data = all_data['gang_of_bandits_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result,
            'open_time': current_line['open_time']
        })
    result['special'] = special
    root['gang_of_bandits_control'] = result
    return 'success', ''


def gen_butch_brothers_control(all_data, root):
    current_data = all_data['butch_brothers_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['butch_brothers_control'] = result
    return 'success', ''


def gen_search_butch_brothers_control(all_data, root):
    current_data = all_data['search_butch_brothers_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['search_butch_brothers_control'] = result
    return 'success', ''


def gen_butcher_house_control(all_data, root):
    current_data = all_data['butcher_house_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['butcher_house_control'] = result
    return 'success', ''


def gen_search_butcher_house_control(all_data, root):
    current_data = all_data['search_butcher_house_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['search_butcher_house_control'] = result
    return 'success', ''


def gen_monument_show_control(all_data, root):
    current_data = all_data['monument_show_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['monument_show_control'] = result
    return 'success', ''


def gen_kingdom_old_and_new_server(all_data, root):
    current_data = all_data['kingdom_old_and_new_server']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['kingdom_old_and_new_server'] = result
    return 'success', ''


def gen_recommend_sid_control(all_data, root):
    current_data = all_data['recommend_sid_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'recommend_rule': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['recommend_sid_control'] = result
    return 'success', ''


def gen_ammunition_supplement(all_data, root):
    current_data = all_data['ammunition_supplement']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['ammunition_supplement'] = result
    return 'success', ''


def gen_medal_open_control_v3(all_data, root):
    current_data = all_data['medal_open_control_v3']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['medal_open_control_v3'] = result
    return 'success', ''


def gen_medal_id_open_control(all_data, root):
    current_data = all_data['medal_id_open_control']
    result = {}

    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        medal_id = str(current_line['medal_id'])
        if medal_id in result:
            return 'fail', f"[表格第 {index + 1} 行] 勋章ID[{current_line['medal_id']}]与之前的配置有重复"
        else:
            result[medal_id] = sid_parsed_result

    root['medal_id_open_control'] = result
    return 'success', ''


def gen_vip_lv_open_control(all_data, root):
    current_data = all_data['vip_lv_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['rank_id'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['vip_lv_open_control'] = result
    return 'success', ''


def gen_growth_mission(all_data, root):
    current_data = all_data['growth_mission']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['growth_mission'] = result
    return 'success', ''


def gen_back_flow_config(all_data, root):
    current_data = all_data['back_flow_config']
    result = {}

    result['cool_down_time'] = current_data['cooldown_time']

    root['back_flow_config'] = result
    return 'success', ''


def gen_west_school_open_control(all_data, root):
    current_data = all_data['west_school_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['west_school_open_control'] = result
    return 'success', ''


def gen_school_instructor_open_control(all_data, root):
    current_data = all_data['school_instructor_open_control']
    result = {}

    result['default'] = []

    sid_map = {}
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"

        school_id = str(current_line['instructor_id'])
        for sid in sid_parsed_result:
            if sid not in sid_map:
                sid_map[sid] = str(school_id)
            else:
                sid_map[sid] += (',' + str(school_id))

    school_map = {}
    for sid in sid_map:
        school_id = sid_map[sid]
        if school_id not in school_map:
            school_map[school_id] = []
        school_map[school_id].append(int(sid))

    ans = []
    for school in school_map:
        school_list = school.split(',')
        _open = []
        for _id in school_list:
            _open.append(int(_id))
        sid_list = school_map[school]

        _dict = {
            'open': _open,
            'sid_list': sid_list
        }
        ans.append(_dict)

    open_control = all_data["west_school_open_control"]
    open_sid_list = []
    not_open_sid_list = []
    if open_control['default'] != 1:
        for node in open_control['list']:
            if node['open'] == 1:
                open_sid_list += ToSidList(node['sid_list'])
            else:
                not_open_sid_list += ToSidList(node['sid_list'])
    open_sid_list = list(set(open_sid_list))

    flag = True
    for node in ans:
        for sid in node['sid_list']:
            if sid not in open_sid_list:
                flag = False
                return 'fail', f"表格解析服务器失败(model=1) west_school_open_control sid[{sid}]未开放"
            if sid in not_open_sid_list:
                flag = False
                return 'fail', f"表格解析服务器失败(model=2) west_school_open_control sid[{sid}]未开放"
    if flag:
        result['special'] = ans
        root['west_school_instructor_open_control'] = result
        return 'success', ''


def gen_crystal_synthesis_lv_control(all_data, root):
    current_data = all_data['crystal_synthesis_lv_control']
    result = {}

    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        crystal_type = str(current_line['type'])
        lv = current_line['max_level']
        if crystal_type not in result:
            result[crystal_type] = []

        result[crystal_type].append({
            'sid_list': sid_parsed_result,
            'lv': lv
        })

    root['crystal_synthesis_lv_control'] = result
    return 'success', ''


def gen_mysterious_merchant_control(all_data, root):
    current_data = all_data['mysterious_merchant_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['mysterious_merchant_control'] = result
    return 'success', ''


def gen_sort_welfare_control(all_data, root):
    current_data = all_data['sort_welfare_control']
    result = []

    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        result.append({
            'type': current_line['event_type'],
            'pic': current_line['open']
        })

    root['sort_welfare_control'] = result
    return 'success', ''


def gen_new_user_guide(all_data, root):
    current_data = all_data['new_user_guide']
    result = {}

    result['default'] = []

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"

        open_list = [
            current_line['show_0'], current_line['show_1'],
            current_line['show_2'], current_line['show_3'],
            current_line['show_4'], current_line['show_5'],
            current_line['show_6'], current_line['show_7'],
            current_line['show_8'], current_line['show_9']
        ]

        if sid_parsed_result == [-1]:
            result['default'] = open_list
        else:
            special.append({
                'open': open_list,
                'sid_list': sid_parsed_result
            })

    result['special'] = special
    root['new_user_guide'] = result
    return 'success', ''


def gen_new_user_show_iap(all_data, root):
    current_data = all_data['new_user_show_iap']
    result = {}

    result['default'] = []

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"

        open_list = [
            current_line['show_0'], current_line['show_1'],
            current_line['show_2'], current_line['show_3'],
            current_line['show_4'], current_line['show_5'],
            current_line['show_6'], current_line['show_7'],
            current_line['show_8'], current_line['show_9']
        ]

        if sid_parsed_result == [-1]:
            result['default'] = open_list
        else:
            special.append({
                'show': open_list,
                'sid_list': sid_parsed_result
            })

    result['special'] = special
    root['new_user_show_iap'] = result
    return 'success', ''


def gen_cowboy_berserker_control(all_data, root):
    current_data = all_data['cowboy_berserker_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['cowboy_berserker_control'] = result
    return 'success', ''


def gen_evip_control(all_data, root):
    current_data = all_data['evip_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['evip_control'] = result
    return 'success', ''


def gen_pet_system_open_control(all_data, root):
    current_data = all_data['pet_system_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['pet_system_open_control'] = result
    return 'success', ''


def gen_pet_delivery_open_control(all_data, root):
    current_data = all_data['pet_delivery_open_control']
    result = {}
    if not current_data['default']:
        default_result = []
    else:
        (default_success, default_result) = SafeToSidList(current_data['default'])
        if default_success is False:
            return 'fail', f"默认配置解析失败：{current_data['default']}"
    result['default'] = default_result

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        (open_parsed_success, open_parsed_result) = SafeToSidList(current_line['open'])
        if open_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析宠物ID数组失败：{current_line['open']}"
        special.append({
            'open': open_parsed_result,
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['pet_delivery_open_control'] = result
    return 'success', ''


def gen_special_effects_open_control(all_data, root):
    current_data = all_data['special_effects_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['westgame_special_effects_open_control'] = result
    return 'success', ''


def gen_cowboy_id_open_control(all_data, root):
    current_data = all_data['cowboy_id_open_control']
    result = {}

    if not current_data['default']:
        default = []
    else:
        (default_parsed_success, default) = SafeToSidList(
            current_data['default'])
        if default_parsed_success is False:
            return 'fail', f"解析默认配置失败：{current_data['default']}"
    result['default'] = default

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"

        if current_line['open']:
            (open_parsed_success, open_parsed_result) = SafeToSidList(current_line['open'])
            if open_parsed_success is False:
                return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['open']}"
        else:
            open_parsed_result = []
        special.append({
            'open': open_parsed_result,
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['cowboy_id_open_control'] = result
    return 'success', ''


def gen_fetter_id_open_control(all_data, root):
    current_data = all_data['fetter_id_open_control']
    result = {}

    if not current_data['default']:
        default = []
    else:
        (default_parsed_success, default) = SafeToSidList(
            current_data['default'])
        if default_parsed_success is False:
            return 'fail', f"解析默认配置失败：{current_data['default']}"
    result['default'] = default

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"

        if current_line['open']:
            (open_parsed_success, open_parsed_result) = SafeToSidList(current_line['open'])
            if open_parsed_success is False:
                return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['open']}"
        else:
            open_parsed_result = []
        special.append({
            'open': open_parsed_result,
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['fetter_id_open_control'] = result
    return 'success', ''


def gen_cowboy_proficiency_open_control(all_data, root):
    current_data = all_data['cowboy_proficiency_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['cowboy_proficiency_open_control'] = result
    return 'success', ''


def gen_cowboy_sort_button_open_control(all_data, root):
    current_data = all_data['cowboy_sort_button_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['cowboy_sort_button_open_control'] = result
    return 'success', ''


def gen_account_switch_open_control(all_data, root):
    current_data = all_data['account_switch_open_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['account_switch_open_control'] = result
    return 'success', ''


def gen_hero_explore_control(all_data, root):
    current_data = all_data['hero_explore_control']
    result = {}

    result['default'] = int(current_data['default'])

    special = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析服务器失败：{current_line['sid_list']}"
        special.append({
            'open': int(current_line['open']),
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['hero_explore_control'] = result
    return 'success', ''


def gen_military_animal_fetter_id_open_control(all_data, root):
    current_data = all_data['military_animal_fetter_id_open_control']
    result = {}

    (default_parsed_success, default_parsed_result) = SafeToSidList(current_data['default'])
    if default_parsed_success is False:
        return 'fail', f"解析默认配置失败：{current_data['default']}"
    if -1 in default_parsed_result:
        return 'fail', f"默认配置：{current_data['default']} 不允许填 -1"
    result['default'] = default_parsed_result

    special = []
    sid_pd = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (open_parsed_success, open_parsed_result) = SafeToSidList(current_line['open'])
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if open_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析 羁绊ID列表 失败：{current_line['open']}"
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析 服务器列表 失败：{current_line['sid_list']}"

        for i in sid_parsed_result:
            if i in sid_pd:
                return 'fail', f"[表格第 {index + 1} 行] 服务器校验失败：{current_line['sid_list']} 重复的 sid：{i}"
            sid_pd.append(i)

        special.append({
            'open': open_parsed_result,
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['military_animal_fetter_id_open_control'] = result
    return 'success', ''


def gen_scroll_id_open_control(all_data, root):
    current_data = all_data['scroll_id_open_control']
    result = {}

    (default_parsed_success, default_parsed_result) = SafeToSidList(current_data['default'])
    if default_parsed_success is False:
        return 'fail', f"解析默认配置失败：{current_data['default']}"
    if -1 in default_parsed_result:
        return 'fail', f"默认配置：{current_data['default']} 不允许填 -1"
    result['default'] = default_parsed_result

    special = []
    sid_pd = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (open_parsed_success, open_parsed_result) = SafeToSidList(current_line['open'])
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if open_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析 套装ID列表 失败：{current_line['open']}"
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析 服务器列表 失败：{current_line['sid_list']}"

        for i in sid_parsed_result:
            if i in sid_pd:
                return 'fail', f"[表格第 {index + 1} 行] 服务器校验失败：{current_line['sid_list']} 重复的 sid：{i}"
            sid_pd.append(i)

        special.append({
            'open': open_parsed_result,
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['scroll_id_open_control'] = result
    return 'success', ''


def gen_sheriff_level_limit_control(all_data, root):
    current_data = all_data['sheriff_level_limit_control']
    result = {}

    result['default'] = current_data['default']

    special = []
    sid_pd = []
    for index in range(len(current_data['list'])):
        current_line = current_data['list'][index]
        (sid_parsed_success, sid_parsed_result) = SafeToSidList(current_line['sid_list'])
        if sid_parsed_success is False:
            return 'fail', f"[表格第 {index + 1} 行] 解析 服务器列表 失败：{current_line['sid_list']}"
        for i in sid_parsed_result:
            if i in sid_pd:
                return 'fail', f"[表格第 {index + 1} 行] 服务器校验失败：{current_line['sid_list']} 重复的 sid：{i}"
            sid_pd.append(i)

        special.append({
            'open': current_line['open'],
            'sid_list': sid_parsed_result
        })
    result['special'] = special
    root['sheriff_level_limit_control'] = result
    return 'success', ''


generator_map = {
    'troop_control': gen_troop_control,
    'event_svr_conf': gen_event_svr_conf,
    'treasure_control': gen_treasure_control,
    'a3_treasure_open_control': gen_a3_treasure_open_control,
    'rebel_control': gen_rebel_control,
    'search_rebel_control': gen_search_rebel_control,
    'gang_of_bandits_control': gen_gang_of_bandits_control,
    'butch_brothers_control': gen_butch_brothers_control,
    'search_butch_brothers_control': gen_search_butch_brothers_control,
    'butcher_house_control': gen_butcher_house_control,
    'search_butcher_house_control': gen_search_butcher_house_control,
    'monument_show_control': gen_monument_show_control,
    'kingdom_old_and_new_server': gen_kingdom_old_and_new_server,
    'recommend_sid_control': gen_recommend_sid_control,
    'ammunition_supplement': gen_ammunition_supplement,
    'medal_open_control_v3': gen_medal_open_control_v3,
    'medal_id_open_control': gen_medal_id_open_control,
    'vip_lv_open_control': gen_vip_lv_open_control,
    'growth_mission': gen_growth_mission,
    'back_flow_config': gen_back_flow_config,
    'west_school_open_control': gen_west_school_open_control,
    'school_instructor_open_control': gen_school_instructor_open_control,
    'crystal_synthesis_lv_control': gen_crystal_synthesis_lv_control,
    'mysterious_merchant_control': gen_mysterious_merchant_control,
    'sort_welfare_control': gen_sort_welfare_control,
    'new_user_guide': gen_new_user_guide,
    'new_user_show_iap': gen_new_user_show_iap,
    'cowboy_berserker_control': gen_cowboy_berserker_control,
    'evip_control': gen_evip_control,
    'pet_system_open_control': gen_pet_system_open_control,
    'pet_delivery_open_control': gen_pet_delivery_open_control,
    'special_effects_open_control': gen_special_effects_open_control,
    'cowboy_id_open_control': gen_cowboy_id_open_control,
    'fetter_id_open_control': gen_fetter_id_open_control,
    'cowboy_proficiency_open_control': gen_cowboy_proficiency_open_control,
    'cowboy_sort_button_open_control': gen_cowboy_sort_button_open_control,
    'account_switch_open_control': gen_account_switch_open_control,
    'hero_explore_control': gen_hero_explore_control,
    'military_animal_fetter_id_open_control': gen_military_animal_fetter_id_open_control,
    'scroll_id_open_control': gen_scroll_id_open_control,
    'sheriff_level_limit_control': gen_sheriff_level_limit_control
}
